<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        .header{
            background: yellow;
            width: 100%;
            height: 100px;

        }
        .nav{
            background: red;
            width: 500px;
            height: 50px;
            margin: 0 auto;
            position: sticky;  /*粘性定位可设置负值 */
            top: 0px;
        }
        .body{
            height: 1000px;
            background: green;
        }
    </style>
</head>
<body>
    <div class="header"></div>
    <div class="nav"></div>
    <div class="body"></div>
</body>
</html>